import { Component, OnInit, Input } from '@angular/core';
import { MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService, DialogService } from 'primeng/primeng';
import { FreeflowService } from '../freeflow.service';
import { FfActAttrComponent } from '../act/ffact-attr.component';
import { FfActFilterComponent } from '../act/ffact-filter.component';
import { FfActEventComponent } from '../act/ffact-event.component';

@Component({
  selector: 'ff-act',
  templateUrl: './ffact.component.html',
  styleUrls: ['./ffact.component.scss'],
  providers: [DialogService]
})
export class FfActComponent implements OnInit {
  currentProcDefId: number = 0;
  @Input()
  set procDefId(procDefId: number) {
    if (!procDefId) return;
    this.currentProcDefId = procDefId;
    this.freeflowService.listProcActDef(procDefId).subscribe(acts => this.acts = acts);
  }
  get procDefId(): number { return this.currentProcDefId; }

  acts: any[];
  selectedAct: any;
  cols: any[] = [
    { field: 'actDefId', header: '编号', rowspan: 1, style: { 'width': '10%', 'text-align': 'center' } },
    { field: 'actDefName', header: '名称', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'actTypeName', header: '节点类型', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'actPropertyName', header: '节点属性', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'warnTime', header: '警告时限', rowspan: 1, style: { 'width': '15%', 'text-align': 'center' } },
    { field: 'limitTime', header: '截止时限', rowspan: 1, style: { 'width': '15%', 'text-align': 'center' } }
  ];

  constructor(
    private freeflowService: FreeflowService,
    private dialogService: DialogService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) {
  }

  ngOnInit() {
  }

  /**移除工作流节点 */
  remove(data: any) {
    this.confirmationService.confirm({
      header: '确认',
      message: '您确定要删除该节点吗？',
      accept: () => {
        this.freeflowService.deleteActDef(data['actDefId']).subscribe(data => {
          this.acts.forEach((element, i) => {
            if (element['actDefId'] == data) {
              this.acts.splice(i, 1);
            }
          });
          this.mdlSnackbarService.showToast('数据删除成功！');
        });
      }
    });
  }
  /**编辑属性 */
  editAttribute($event) {
    const ref = this.dialogService.open(FfActAttrComponent, {
      header: '环节属性',
      width: '550px',
      contentStyle: { 'height': '560px' },
      data: {
        'actDefId': this.selectedAct['actDefId']
      }
    });
    ref.onClose.subscribe(data => data && this.refreshActList());
  }
  /**编辑过滤条件 */
  editFilter($event) {
    const ref = this.dialogService.open(FfActFilterComponent, {
      header: '过滤条件',
      width: '500px',
      contentStyle: { 'height': '480px' },
      data: {
        'actDefId': this.selectedAct['actDefId']
      }
    });
    ref.onClose.subscribe(data => data && this.refreshActList());
  }
  /**编辑节点事件 */
  editEvent($event) {
    const ref = this.dialogService.open(FfActEventComponent, {
      header: '节点事件',
      width: '650px',
      contentStyle: { 'height': '500px' },
      data: {
        'actDefId': this.selectedAct['actDefId']
      }
    });
    ref.onClose.subscribe(data => data && this.refreshActList());
  }

  refreshActList() {
    this.freeflowService.listProcActDef(this.currentProcDefId).subscribe(acts => this.acts = acts);
  }
}
